import { _decorator, Button, Node } from 'cc';
import { BaseUI } from '../../../A-FRAME/component/ui.base';
import { UserData } from '../../../Data/UserData';
import { Guide } from '../../../game/Guide';
import InitData from '../../../game/InitData';
import { AudioMgr } from '../../../Mgr/AudioMgr';
import { BoardMgr } from '../../../Mgr/BoardMgr';
import { GameMgr } from '../../../Mgr/GameMgr';
import { UIMgr } from '../../../Mgr/UIMgr';
import { Url } from '../../../Tools/Url';
import { ClientAPI } from '../../api/client.api';
import { WithdrawPage } from '../pop-up/popup.withdrawpage';
import { Popup } from './popup';
const { ccclass, property } = _decorator;

@ccclass('PageHome')
export class PageHome extends BaseUI {

    @property(Node)
    withdrawButton: Node = null;
    @property(Node)
    challengeButton: Node = null;

    @property(Node)
    homePage: Node = null;
    @property(Node)
    gamePanel: Node = null;
    @property(Node)
    withdrawPage: Node = null;
    @property(Node)
    lottoryPage: Node = null;
    @property(Node)
    lightButton: Node = null

    lightLabel: Node = null;
    lightadIcon: Node = null;
    lightBtnM: Node = null;


    protected initAfterOnLoad(): void {
        this.lightLabel = this.lightButton.getChildByName('Label');
        this.lightadIcon = this.lightButton.getChildByName('adicon');
        this.lightBtnM = this.lightButton.getChildByName('button-light1');
    }

    async showWithDrawPage() {
        AudioMgr.ins.playOneShot(Url.AUDIO.SFX2, 1);
        Promise.resolve(InitData.getWithdrawConf())
            .then(async conf => {
                let count = 0;
                while (!conf && count < 3) {
                    conf = await InitData.getWithdrawConf(true);
                    count++;
                }
                if (conf) {
                    this.withdrawButton.getChildByName('button-nose').active = false;
                    this.challengeButton.getChildByName('button-nose').active = true;
                    this.homePage.active = false;
                    this.gamePanel.active = false;
                    this.withdrawPage.getComponent(WithdrawPage).pageConfig = conf;
                    this.withdrawPage.active = true;
                } else {
                    UIMgr.ins.createOneTipToast("network error");
                    console.log('提现奖励---请求失败:');
                }
            });
    }

    async showLotteryPage() {
        GameMgr.ins.resetTimer();
        AudioMgr.ins.playOneShot(Url.AUDIO.SFX2, 1);
        this.lottoryPage.active = true;
    }

    showChallengePanel() {
        if (UserData.data.isCommer && UserData.data.guideRecord == 6) {
            UIMgr.ins.guideNode.active = true;
            Guide.ins.completeCurGuide(7)
        }
        GameMgr.ins.startTimer();
        AudioMgr.ins.playOneShot(Url.AUDIO.SFX2, 1);
        this.withdrawButton.getChildByName('button-nose').active = true;
        this.challengeButton.getChildByName('button-nose').active = false;
        this.homePage.active = true;
        this.gamePanel.active = true;
        this.withdrawPage.active = false;
    }


    protected _click_event_: { [name: string]: (this: PageHome, button: Button) => void; } = {
        async cashbtn(btn) {
            if (Popup.lock) {
                return void 0;
            }
            Popup.lock = true;
            if (UserData.data.isCommer && UserData.data.guideRecord == 6) {
                Guide.ins.tempCloseGuide();
            }
            GameMgr.ins.resetTimer();
            this.showWithDrawPage();
            Popup.lock = false;
        },
        async WithdrawButton(btn) {
            if (Popup.lock) {
                return void 0;
            }
            Popup.lock = true;

            GameMgr.ins.resetTimer();
            if (UserData.data.isCommer && UserData.data.guideRecord == 4) {
                Guide.ins.tempCloseGuide();
            }
            GameMgr.ins.resetTimer();
            this.showWithDrawPage();
            Popup.lock = false;
        },
        ChallengeButton() {
            if (Popup.lock) {
                return void 0;
            }
            Popup.lock = true;
            GameMgr.ins.startTimer();
            if (UserData.data.isCommer && UserData.data.guideRecord == 6) {
                UIMgr.ins.guideNode.active = true;
                Guide.ins.completeCurGuide(7)
            }
            AudioMgr.ins.playOneShot(Url.AUDIO.SFX2, 1);
            this.withdrawButton.getChildByName('button-nose').active = true;
            this.challengeButton.getChildByName('button-nose').active = false;
            this.homePage.active = true;
            this.gamePanel.active = true;
            this.withdrawPage.active = false;
            Popup.lock = false;

        },
        async LightButton() {
            if (Popup.lock) {
                return void 0;
            }
            Popup.lock = true;
            if (UserData.data.propNum > 0) {
                if (GameMgr.ins.usePropTimes != 0) {
                    if (UserData.data.isCommer) {
                        if (UserData.data.guideRecord === 7) {
                            Guide.ins.tempCloseGuide();
                            UserData.data.isCommer = false;
                        }
                        GameMgr.ins.usePropTimes = 0;
                        BoardMgr.ins.showAllAnswerFristWord();

                    } else {
                        GameMgr.ins.usePropTimes = 0;
                        BoardMgr.ins.showAllAnswerFristWord();
                        UserData.data.propNum--;
                    }
                } else {
                    UIMgr.ins.createOneTipToast('game_tip_text8');
                }
            } else {
                if (GameMgr.ins.usePropTimes != 0) {
                    let isCom = await ClientAPI.AD("incentive");
                    if (isCom) {
                        UserData.data.propNum++;
                    }
                } else {
                    UIMgr.ins.createOneTipToast('game_tip_text8');
                }

            }
            UIMgr.ins.freshLightState();
            Popup.lock = false;
        },
        RefreshButton() {
            if (Popup.lock) {
                return void 0;
            }
            Popup.lock = true;
            AudioMgr.ins.playOneShot(Url.AUDIO.SFX2, 1);
            GameMgr.ins.resetTimer();
            Popup.Refreshpage()
            Popup.lock = false;
        },
        async SettingButton(btn) {
            if (Popup.lock) {
                return void 0;
            }
            Popup.lock = true;
            AudioMgr.ins.playOneShot(Url.AUDIO.SFX2, 1);
            GameMgr.ins.resetTimer();
            await Popup.Settingpage({});
            Popup.lock = false;
        },
        LotteryButton(btn) {
            if (Popup.lock) {
                return void 0;
            }
            Popup.lock = true;
            GameMgr.ins.resetTimer();
            this.showLotteryPage();
            Popup.lock = false;
        }

    }

}

